// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-12-6

#include "stdafx.h"
#include "base_time.h"

namespace JoyServer
{
;
void JoyGetTimeSpan(CJoyTimeSpan& span, UINT64 s)
{
    span.m_milli    = (UINT)s % 1000;
    s   /= 1000;

    span.m_second   = (UINT)s % 60;
    s   /= 60;

    span.m_minute   = (UINT)s % 60;
    s   /= 60;

    span.m_hour     = (UINT)s;
    //span.m_hour     = (UINT)s % 24;
    //s   /= 24;

    //span.m_day      = (UINT)s;
}

void JoyGetTimeSpan(CJoyTimeSpan& span, const FILETIME& start, const FILETIME& end)
{
    UINT64 s = (*(UINT64*)&end / 10000) - (*(UINT64*)&start / 10000);

    JoyGetTimeSpan(span, s);
}

void JoyGetTimeSpan(CJoyTimeSpan& span, const SYSTEMTIME& start, const SYSTEMTIME& end)
{
    FILETIME s, e;
    SystemTimeToFileTime(&start, &s);
    SystemTimeToFileTime(&end, &e);
    JoyGetTimeSpan(span, s, e);
}

void JoyGetCurrentFileTime(FILETIME& curr)
{
    SYSTEMTIME st;
    GetLocalTime(&st);
    SystemTimeToFileTime(&st, &curr);
}

UINT64 JoyGetCurrentTick()
{
    FILETIME curr;
    SYSTEMTIME st;
    GetLocalTime(&st);
    SystemTimeToFileTime(&st, &curr);

    return (*(UINT64*)&curr / 10000);
}

void JoyGetProcessTimes(UINT64& uKernelTime, UINT64& uUserTime)
{
    FILETIME creationTime, exitTime, kernelTime, userTime;

    GetProcessTimes(GetCurrentProcess(),
        &creationTime,
        &exitTime,
        &kernelTime,
        &userTime);

    uKernelTime = (*(UINT64*)&kernelTime / 10000);
    uUserTime   = (*(UINT64*)&userTime / 10000);
}

} // namespace JoyServer